//
//  ComposeViewController.m
//  01 微博
//
//  Created by zhoup on 16/5/23.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "ComposeViewController.h"
#import "PicCollectionViewController.h"

#import "WPTextView.h"
#import "WPComposeToolBar.h"


@interface ComposeViewController ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *inputView;

@property (weak, nonatomic) IBOutlet UIView *inputViewContainer;


@property(nonatomic,strong) WPComposeToolBar *toolBar;
@property(nonatomic,strong) WPTextView *wpInputView;

@property (strong, nonatomic) PicCollectionViewController *containVc;

@end

@implementation ComposeViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor redColor]];
    [self setNav];
    
    [self setInputView];
    
    [self setToolbar];
    
}

-(void)setToolbar
{
    WPComposeToolBar *toolBar = [[WPComposeToolBar alloc]init];
    toolBar.frame = CGRectMake(0, self.view.height - 44, self.view.width, 44);
    
    [self.view addSubview:toolBar];
    self.toolBar = toolBar;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)keyBoardShow:(NSNotification *)notification
{
    NSLog(@"%@",notification.userInfo);
    NSDictionary *info = notification.userInfo;
    CGFloat times = [info[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    NSInteger animationCurve = [info[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    CGRect rect = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = rect.size.height;
    [UIView animateWithDuration:times delay:0 options:animationCurve << 16 animations:^{
        self.toolBar.y = self.view.height -height-44;
    } completion:^(BOOL finished) {
    }];
}

-(void)keyBoardHide:(NSNotification *)notification
{
    NSLog(@"%@",notification.userInfo);
    NSLog(@"%@",notification.userInfo);
    NSDictionary *info = notification.userInfo;
    
    CGFloat times = [info[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    NSInteger animationCurve = [info[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    [UIView animateWithDuration:times delay:0 options:animationCurve << 16 animations:^{
        self.toolBar.y = self.view.height - 44;
    } completion:^(BOOL finished) {
        
    }];
}

/*
  设置导航控制器
 */
-(void)setNav
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(compose)];
    
}

-(void)setInputView
{
    NSLog(@"%@",NSStringFromCGRect(self.inputView.frame));
    WPTextView *textView = [[WPTextView alloc]init];
    textView.frame = self.inputView.bounds;
    
    
    textView.placeholder = @"写点东西吧";
    // 默认清空下TextView是不能滚动的, 但是可以通过设置某些属性让其默认就可以滚动
    textView.alwaysBounceVertical = YES;
    [self.inputView addSubview:textView];
    
    self.wpInputView = textView;
    textView.delegate = self;
}

- (void)close {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UITextViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 关闭键盘
    [self.view endEditing:YES];
}

#pragma mark 发送微博
-(void)compose
{
    if (self.containVc.images.count == 0) {
        [self sendWeiboInfoWithOutImage];
    }
    else
    {
        [self sendWeiboInfoWithImage];
    }
}


-(void)sendWeiboInfoWithOutImage
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSString *url = @"https://api.weibo.com/2/statuses/update.json";
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    parameters[@"access_token"] = [WPAccount accountFromSandbox].access_token;
    parameters[@"status"] = self.wpInputView.text;
    
    [manager POST:url parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"success");
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"failer");
    }];

    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)sendWeiboInfoWithImage
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSString *url =  @"https://upload.api.weibo.com/2/statuses/upload.json";
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    // 2.封装请求参数
    parameters[@"access_token"] = [WPAccount accountFromSandbox].access_token;
    parameters[@"status"] = self.wpInputView.text;
    
    [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        // 1.取出图片
        UIImage *image = [self.containVc.images firstObject];
        // 2.将图片转换为二进制
        NSData *iamgeData = UIImagePNGRepresentation(image);
        [formData appendPartWithFileData:iamgeData name:@"pic" fileName:@"pic.png" mimeType:@"image/png"];
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"suceess");
        [self dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"fail");
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    PicCollectionViewController *picVc =segue.destinationViewController;
    
    self.containVc = picVc;
}
@end
